package com.onec.service.stream;

import com.onec.service.stream.util.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

public class PersonGenerator {

    static Logger logger = LoggerFactory.getLogger(PersonGenerator.class);

    public static List<Person> createPersonList() {

        List<Person> personList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 200; i++) {
            Person person = new Person();
            person.setAvg(String.valueOf(random.nextDouble() * i));
            person.setAvg("name" + random.nextDouble());
            personList.add(person);
        }
        return personList;
    }

    public static void main(String[] args) {
        List<Person> personList = createPersonList();
        Map<String, List<Person>> keyList = personList
                .parallelStream()
                .collect(Collectors.groupingByConcurrent(Person::getName));

        keyList.keySet().forEach(key -> {
            List<Person> people = keyList.get(key);
            logger.info("---->{}", people);
        });

        // 链接起来
        String message = personList
                .parallelStream()
                .map(p -> p.toString())
                .collect(Collectors.joining(","));

//        //
//        Map<Boolean, List<Person>> booleanListMap = personList.parallelStream().collect(Collectors.partitioningBy(
//                person -> person.getAvg().equals("1000")
//        ));
//
//        ConcurrentHashMap<String, String> keyName = personList
//                .parallelStream()
//                .collect(Collectors.toConcurrentMap(p -> p.getName(), p -> p.getClass(), (s1, s2) -> s1.toString() + s2.toString()));


//        keyName.forEach((key, value) ->
//                logger.info("key:{},value:{}", key, value)
//        );


        personList.parallelStream().collect(ArrayList::new, (list, person) -> {
            if ("1".equals(person.getName())) {
                list.add(person.getName());
            }
        }, ArrayList::addAll);
    }


}
